<?php
/*script posting a game result to feed, takes following GET parameters:
 *poster_ip - an IP of the device that is posting the message
 *postee_id - an IP of the device that the message will be posted to
 *result - either a (win), b (draw), c (failure)
 */
ini_set( "display_errors", 0);
include 'configuration.php';

if(isset($_GET['postee_ip']) && isset($_GET['result']) && isset($_GET['poster_ip']) && isset($_GET['location'])) 
{		
	//if all params set, post
	$postee_ip = $_GET['postee_ip'];
	$poster_ip = $_GET['poster_ip'];
	$res = $_GET['result'];
	$loc = $_GET['location'];
	
	//retrieve the access token
	$link = mysql_connect('localhost', $DBUSER, $DBPASS) or die('Could not connect: ' . mysql_error());
	mysql_select_db('facebook') or die('Could not select database');
	
	$query = "select access_token from users where address='$poster_ip'";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_NUM);
	$token = $row[0];
	
	$query = "select id from users where address='$postee_ip'";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_NUM);
	$postee_id = $row[0];
	
	$l = "http://mandurah.inf.ed.ac.uk";
	$c = "Play it yourself!";
	
	if ($res=="a")
	{
		$n = "I won the game of Rock-Paper-Scissors in $loc! How exciting!";	
		$d = "I won the game and I'm not afraid of posting it! I hope you all see how good I am at rock-paper-scissors and admire me.";
		
		$query = "select score from users where address='$poster_ip'";
		$result = mysql_query($query) or die('Query failed: ' . mysql_error());
		$row = mysql_fetch_array($result, MYSQL_NUM);
		$score = $row[0];
		$score += 1;
		
		$query = "update users set score=$score where address='$poster_ip'";
		$result = mysql_query($query) or die('Query failed: ' . mysql_error());
		
	}
	else if ($res=="b")
	{
		$n = "I tied the game of Rock-Paper-Scissors in $loc.";	
		$d = "I played the game of rock-paper-scissors and it was a tie. It's not the result that counts.";
	}
	else if ($res=="c")
	{
		$n = "I lost the game of Rock-Paper-Scissors in $loc. So what?";	
		$d = "Well, I might have lost, but what kind of game was that anyway? Rock-paper-scissors? How lame is that?";
	}
	else
	{
		echo 'fail';
		exit();
	}
	//echo "Sending message '$message' to user id $postee_id with token $token</br>\n";
	
	//send the message
	do_post_request("https://graph.facebook.com/$postee_id/feed", "access_token=$token&link=$l&caption=$c&name=$n&description=$d");
	
	mysql_free_result($result);
	mysql_close($link);
}

function do_post_request($url, $data)
{
	//sends a post request to $url with post data $data
	define('POSTURL', $url);
	define('POSTVARS', $data);  // POST VARIABLES TO BE SENT
	
	$ch = curl_init(POSTURL);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, POSTVARS);
	curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$out = curl_exec($ch);
	$response = json_decode($out);
	if (isset($response->{'error'}))
	{
		echo 'fail';
		echo $out;
	}
	else
	{
		echo 'victory';
	}
	curl_close($ch);
}

?>
